package com.wecheir.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wecheir.dao.RoleDao;
import com.wecheir.dao.UserDao;
import com.wecheir.entity.User;
import com.wecheir.entity.userWithRole.SingleUserWithRole;
import com.wecheir.entity.userWithRole.UserWithRole;
import com.wecheir.service.UserService;
import com.wecheir.utils.ResponseUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    UserDao userDao;
    @Resource
    RoleDao roleDao;

    @Override
    public void updateUserOnlyCredit(User user) {
    }
    @Override
    public String findFarmOwnManagerIdById(String id) {
        return null;
    }
    @Override
    public User findUserByLoginName(String name) {
        return null;
    }
    @Override
    public User findUserById(String id) {
        return userDao.findUserById(id);
    }
    @Override
    public int userCount(String loginName) {
        return 0;
    }
    @Override
    public void saveUser(User user) {
        user.setLocked(false);
        userDao.userAdd(user);
    }
    @Override
    public void updateUser(User user) {
    }
    @Override
    public void deleteUser(User user) {
        userDao.deleteUser(user.getId());
    }
    @Override
    public void lockUser(User user) {
        userDao.updateLockById(user.getId(), user.getLocked() ? 0 : 1);
    }
    @Override
    public List<User> findAllUser() {
        return null;
    }
    @Override
    public Map<String, Object> getUserList(int start, Integer limit) {
//        Map<String ,Object> data = new HashMap<>();
//        List<User> userList = userDao.getUserList(start,limit);
//        Integer count = userDao.getAllCount();
//        data.put("userList",userList);
//        data.put("count",count);
//        return data;
        PageHelper.startPage(start, limit);
        List<User> userList = userDao.getUserList();
        PageInfo<User> userPageInfo = new PageInfo<>(userList);

        Map<String ,Object> data = new HashMap<>();
        data.put("userList",userList);
        data.put("pageInfo",userPageInfo);
        return data;
    }
    @Override
    public User selectUserByTellOrEmail(String tel, String email) {
        System.out.println(tel + email);
        return userDao.selectUserByTellOrEmail(tel,email);
    }
    @Override
    public List<User> getAllHaveManagerIdentityUser() {
        return null;
    }
    @Override
    public int resetCredit(User user) {
        return 0;
    }
    @Override
    public ResponseUtils checkUser(User userParam, User user) {
        if (null==user){
            return ResponseUtils.setFailure("属性值不正确,没有该用户!");
        }
        if (user.getCredit() < 0.3){
            return ResponseUtils.setFailure("信誉值不足，不能访问!");
        }
        if (!userParam.getPassword().equals(user.getPassword())){
            return ResponseUtils.setFailure("密码错误!");
        }
        if (user.getLocked() == true){
            return ResponseUtils.setFailure("账户已被锁定!");
        }
        if (user.getIs_admin() == 0){
            return ResponseUtils.setFailure("账户不是管理用户!");
        }
        return ResponseUtils.setSuccess(user);
    }

    @Override
    public List<UserWithRole> getUserWithRole() {

        List<UserWithRole> userWithRole = userDao.getUserWithRole();
        List<UserWithRole> res = new ArrayList<>(3);

        for(UserWithRole userWithRole1 : userWithRole){
            if(userWithRole1.getIdentity().contains("温度管理员")){
                userWithRole1.setIdentity("温度管理员");

            }else if(userWithRole1.getIdentity().contains("湿度管理员")){
                userWithRole1.setIdentity("湿度管理员");

            }else if(userWithRole1.getIdentity().contains("光照管理员")){
                userWithRole1.setIdentity("光照管理员");

            }else
                continue;

            userWithRole1.setCount(userWithRole1.getUsers().size());
            res.add(userWithRole1);
        }

        return res;

    }

    @Override
    public void updateCreditById(String id, double credit) throws Exception {
        if(userDao.findUserById(id) == null){
            throw new Exception("failed：用户不存在");
        }
        userDao.updateCreditById(id, credit);
    }

    @Override
    public List<SingleUserWithRole> getSingleUserWithRole() {
        List<SingleUserWithRole> singleUserWithRole = userDao.getSingleUserWithRole();

        return singleUserWithRole;

    }
}
